﻿namespace Arms.Application.Modules.People.Customers
{
    using Arms.Application.Modules.Common.DTOs;
    using Arms.Application.Services;
    using Arms.Domain;
    using Arms.Domain.Modules;
    using Arms.Domain.Modules.People;
    using System;
    using System.Linq;

    public class CustomerService : ServiceBase, ICustomerService
    {
        public CustomerService(IUnitOfWork uow) : base(uow) { }

        public int AddCustomer(string name, string phoneNumber)
        {
            Customer customer = new Customer()
            {
                Person = new Person()
                {
                    Contact = new Contact()
                    {
                        FirstName = name,
                        PhoneNumber = phoneNumber
                    }
                }
            };
            base.UnitOfWork.Repository<Customer>().Insert(customer);
            base.UnitOfWork.Commit();
            return customer.ID;
        }

        public int AddCustomer(ContactDTO contact)
        {
            Customer customer = new Customer()
            {
                Person = new Person()
                {
                    Contact = new Contact()
                    {
                        Title = contact.Title,
                        FirstName = contact.FirstName,
                        MiddleName = contact.MiddleName,
                        LastName = contact.LastName,
                        PhoneNumber = contact.PhoneNumber,
                        CellNumber = contact.CellNumber,
                        FaxNumber = contact.FaxNumber,
                        EmailAddress = contact.EmailAddress
                    }
                }
            };
            base.UnitOfWork.Repository<Customer>().Insert(customer);
            base.UnitOfWork.Commit();
            return customer.ID;
        }

        public void UpdateCustomer(int customerID, ContactDTO contact)
        {
            Customer customer = base.UnitOfWork.Repository<Customer>().Find(customerID);

            if (customer != null)
            {
                customer.Person.Contact.Title = contact.Title;
                customer.Person.Contact.FirstName = contact.FirstName;
                customer.Person.Contact.MiddleName = contact.MiddleName;
                customer.Person.Contact.LastName = contact.LastName;
                customer.Person.Contact.PhoneNumber = contact.PhoneNumber;
                customer.Person.Contact.CellNumber = contact.CellNumber;
                customer.Person.Contact.FaxNumber = contact.FaxNumber;
                customer.Person.Contact.EmailAddress = contact.EmailAddress;
                base.UnitOfWork.Repository<Customer>().Update(customer);
                base.UnitOfWork.Commit();
            }
        }


        
    }
}
